<template>
  <div class="home-container">
    <!-- 顶部搜索栏 -->
    <div class="header-search">
      <div class="search-wrap">
        <el-input
          v-model="searchKey"
          placeholder="搜索书名/作者/分类"
          prefix-icon="Search"
          class="search-input"
          @input="handleInput"
        ></el-input>
        <div class="hot-recommend" v-show="showRecommend">
          <p v-for="(item, idx) in hotList" :key="idx" class="recommend-item">{{ item }}</p>
        </div>
      </div>
      <el-button type="primary" @click="handleSearch" class="search-btn">搜索</el-button>
    </div>

    <!-- 主内容布局（侧边栏+网格
    --- 功能：系统首页，包含搜索栏、侧边导航和图书网格
- 特性：
- 顶部搜索栏支持热词推荐（ v-show 控制显示）
- 侧边栏导航（ el-menu ）包含分类/热门/借阅入口
- 复用图书卡片网格（与 Books.vue 类似）
- 用途：用户进入系统后的主交互界面 -->
    <div class="main-content">
      <!-- 侧边栏导航 -->
      <el-aside class="sidebar" width="240px">
        <el-menu default-active="1" class="sidebar-menu">
          <el-menu-item index="1" @click="$router.push('/categories')">
            <el-icon><Document /></el-icon>
            <span>所有分类</span>
          </el-menu-item>
          <el-menu-item index="/hot-books" @click="$router.push('/hot-books')">
            <el-icon><Star /></el-icon>
            <span>热门图书</span>
          </el-menu-item>
          <el-menu-item index="/BorrowBook" @click="$router.push('/BorrowBook')">
            <el-icon><User /></el-icon>
            <span>我的借阅</span>
          </el-menu-item>
          <el-menu-item index="/admin" @click="$router.push('/admin')">
            <el-icon><Setting /></el-icon>
            <span>管理员界面</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 图书卡片网格 -->
      <el-main class="book-grid">      
          <el-row :gutter="24">
w          <el-col :span="8" v-for="book in bookList" :key="book.id">
            <el-card class="book-card" @mouseenter="() => setHovered(book.id, true)" @mouseleave="() => setHovered(book.id, false)">
              <div class="cover" :style="{ backgroundImage: `url(${book.cover})` }"/>
              <h3 class="book-title">{{ book.title }}</h3>
              <p class="book-author">作者：{{ book.author }}</p>
              <p class="book-info">分类：{{ book.category }}</p>
              <el-button v-show="hovered.get(book.id)" type="primary" size="small" class="borrow-btn">立即借阅</el-button>
            </el-card>
          </el-col>
        </el-row>
      </el-main>
    </div>
    <!-- 分页器 -->
    <el-pagination
      :current-page="currentPage"
      @update:current-page="currentPage = $event"
      :page-size="pageSize"
      @update:page-size="pageSize = $event"
      :total="total"
      layout="prev, pager, next"
    ></el-pagination>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { Document, Star, User } from '@element-plus/icons-vue';

// 搜索相关状态
const searchKey = ref('');
const showRecommend = ref(false);
const hotList = ref(['数据结构与算法', '操作系统原理', '数据库系统概念']);

// 卡片悬停状态（使用Map存储每个卡片的id对应状态）
const hovered = ref(new Map());

// 分页相关
const currentPage = ref(1);
const pageSize = ref(4);
const total = ref(20); // 假设总数据量

const handleInput = (e) => {
  showRecommend.value = e.target.value.length > 0;
};

const handleSearch = () => {
  console.log('执行搜索：', searchKey.value);
  showRecommend.value = false;
};

// 悬停状态更新函数
const setHovered = (id, value) => {
  hovered.value.set(id, value);
};

import img1 from '../assets/images/1.png';
import img2 from '../assets/images/2.png';
import img3 from '../assets/images/3.png';
import img4 from '../assets/images/4.png';
const bookList = ref([
  { id: 1, title: '计算机网络', author: '谢希仁', category: '计算机', cover: img1 },
  { id: 2, title: '算法导论', author: 'CLRS', category: '计算机', cover: img2 },
  { id: 3, title: 'Python编程', author: 'Mark Lutz', category: '编程语言', cover: img3 },
  { id: 4, title: '设计模式', author: 'GoF', category: '软件设计', cover: img4 }
]);
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  padding: 24px;
  background-color: #f8f9fa;
}

.header-search {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
  max-width: 1200px;
  margin: 0 auto 32px;
  position: relative;
}

.search-wrap {
  position: relative;
  flex: 1;
}

.search-input {
  flex: 1;
  max-width: 800px;
  height: 48px;
}

.hot-recommend {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 8px 0;
  z-index: 10;
}

.recommend-item {
  padding: 8px 16px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  transition: background 0.2s;
}
.recommend-item:hover {
  background-color: #f5f7fa;
}

.search-btn {
  height: 48px;
  padding: 0 32px;
  background-color: #2196F3;
  border-color: #2196F3;
  font-size: 16px;
}

.main-content {
  display: flex;
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.sidebar {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.sidebar-menu {
  border-right: none;
}

.book-grid {
  flex: 1;
  margin-bottom: 32px;
}

.book-card {
  height: 320px;
  text-align: center;
  padding: 16px;
  transition: transform 0.2s;
  position: relative;
}
.book-card:hover {
  transform: translateY(-4px);
}

.borrow-btn {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity 0.3s ease-in;
}
.book-card:hover .borrow-btn {
  opacity: 1;
}

.cover {
  width: 100%;
  height: 180px;
  background-size: cover;
  background-position: center;
  border-radius: 4px;
  margin-bottom: 12px;
}

.book-title {
  font-size: 16px;
  color: #2c3e50;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.book-author,
.book-info {
  font-size: 14px;
  color: #606266;
  margin-bottom: 4px;
}

/* 分页样式 */
.el-pagination {
  text-align: center;
  margin-top: 24px;
}
.el-pagination .el-pager li {
  border-radius: 4px;
  margin: 0 4px;
}
.el-pagination .el-pager li.active {
  background-color: #2196F3;
  color: white;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .el-col {
    flex: 0 0 50%; /* 对应span=12，占50%宽度 */
    max-width: 50%;
  }
}
@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
  }
  .sidebar {
    width: 100% !important;
  }
  .el-col {
    flex: 0 0 100%; /* 对应span=24，占100%宽度 */
    max-width: 100%;
  }
  .header-search {
    flex-direction: column;
  }
  .search-btn {
    width: 100%;
  }
}
</style>